<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['technician'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['technician'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $categoryDetailArray = array();
  $categoryName        = "";
  $i                   = 0;  
  $categoryId          = isset($_REQUEST['categoryId']) ? $_REQUEST['categoryId'] : 0;

  // after post : start 
  if(isset($_POST['categoryName']))
  {
    if($categoryId > 0)
    {
      $updateCategory = "UPDATE category 
                            SET categoryName  = '".$_POST['categoryName']."'
                          WHERE categoryId    = '".$_REQUEST['categoryId']."'";
      $updateCategoryRes  = mysql_Query($updateCategory);
      if(!$updateCategoryRes)
        die("Update Query Not Inserted : ".mysql_error());
//      else
//        header("Location:item.php?categoryId=$categoryId");
    }
    else
    {
      $insertCategory = "INSERT INTO category (categoryName)
                          VALUE('".$_POST['categoryName']."')";
      $insertCategoryResult = mysql_query($insertCategory);
      if(!$insertCategoryResult)
        die("Insert Query Not Inserted : ".mysql_error());
      else
      {
        echo $categorySelected = mysql_insert_id();	
        exit();
        //header("Location:item.php?categoryId=$categoryId");
      }
    }
  }    
  // after post : end
  
  $selectCategory = "SELECT categoryId,categoryName
                       FROM category
                   ORDER BY categoryName";
  $selectCategoryResult = mysql_query($selectCategory);
  $categoryRow = mysql_num_rows($selectCategoryResult);
  while($categoryRow = mysql_fetch_array($selectCategoryResult))
  {
    $categoryDetailArray[$i]['categoryId']    = $categoryRow['categoryId'];
    $categoryDetailArray[$i]['categoryName']  = $categoryRow['categoryName'];
    
    if($categoryRow['categoryId'] == $categoryId)
    {
      $categoryName  = $categoryRow['categoryName'];  
    }
    $i++;     
  }    
   
  include("./bottom.php");
  $smarty->assign("categoryId",$categoryId);
  $smarty->assign("categoryName",$categoryName);
  $smarty->assign("categoryDetailArray",$categoryDetailArray);
  $smarty->assign("i",$i);
  $smarty->display("categoryAdd.tpl");
}
?>